<?php
class Member extends Controller
{
	private $tbl = 'tbl_members';
 	function Member()
	{
		parent::Controller();
                $this->load->model('member_model');
	}
	
	function index()
	{
		$data['total_member'] = $this->member_model->total_members();
                create_pagination($data['total_member'], 3, config_item('per_page'));
                $data['members'] = $this->member_model->get_members(config_item('per_page'), $this->uri->segment(3));
		 //debug_array($data['member']);exit;
		$data['page_title']	= 'Manage Member';
		$data['page'] = 'member_list_v';
		$this->load->view('main', $data);	
	}
	
	function add()
	{
		if($this->input->post('submit_member'))
		{
			$this->add_edit();	
		}
		$data['page_title']	 = 'Add New Member';
		$data['page'] = 'member_add_v';
		$this->load->view('main', $data);
	}
	
	function edit($id)
	{
		if($this->input->post('submit_member'))
		{
			$this->add_edit($id);	
		}
		//$data['categories'] = $this->general_db_model->getAll($this->tbl, 'member_name', 'parent_id = 0');
		$data['_member'] = $this->general_db_model->getById($this->tbl, 'id', $id);
		$data['page_title']	 = 'Update Member';
		$data['page'] = 'member_add_v';
		$this->load->view('main', $data);	
	}
	
	function add_edit($id = NULL)
	{
		 $data['first_name'] = $this->input->post('first_name');
		 $data['last_name'] = $this->input->post('last_name');
		 $data['email'] = $this->input->post('email');
		 $data['address1'] = $this->input->post('address1');
		 $data['address2'] = $this->input->post('address2');
		 $data['phone1'] = $this->input->post('phone1');
		 $data['phone2'] = $this->input->post('phone2');
		 $data['status'] = $this->input->post('status');
		 $data['last_active'] = $this->input->post('last_active');
		 if($id)
		 {
		 	$this->general_db_model->update($this->tbl, $data, 'id = '.$id);
			$this->session->set_flashdata('success_message', "Member - <strong>".$data['first_name']." ".$data['last_name']." </strong>Successfully updated.");
		 }
		 else
		 {
		 	$this->general_db_model->insert($this->tbl, $data);
		 	$this->session->set_flashdata('success_message', "Member - <strong>".$data['first_name']." ".$data['last_name']." </strong>Successfully added.");
		 }
		 redirect(current_url());
	}
	
	function delete()
	{
	 	$member_id = $this->input->post('member_id');
		$this->general_db_model->delete($this->tbl, 'id = '.$member_id);
	}
         function check_email(){
            $keyword = $this->input->post('email');
            $ckemail = $this->member_model->check_email($keyword);
            echo (count($ckemail)>0)?"false":"true";
        }
}

?>